package com.shujujiegou.链表.双向链表;

/**
 * 我们知道单向链表只能从一个方向遍历，那么双向链表它可以从两个方向遍历。
 */
public class TwoWayLinkedList {
    private Node head;
    private Node real;
    private Integer size;

    public TwoWayLinkedList() {
        this.head = null;
        this.real =null;
        this.size = 0;
    }

    //在链表头增加节点
    public void addHead(Object data) {
        Node node = new Node(data);
        if(isEmpty()) {
            this.head = node;
            this.real = node;
        } else {
            node.next = this.head;
            this.head.pre = node;
            this.head = node;
        }

        this.size ++;
    }

    //在链表尾增加节点
    public void addLast(Object data) {
        Node node = new Node(data);
        if(isEmpty()) {
            this.head = node;
            this.real = node;
        } else {
            node.pre = this.real;
            this.real.next = node;
            this.real = node;
        }

        this.size ++;
    }

    //删除链表头
    public void deleteHead() {
        if(!isEmpty()) {
            this.head = this.head.next;
            if(this.size != 1) {
                this.head.pre = null;
            } else {
                this.real = this.real.next;
            }
            this.size --;
        }
    }

    //删除链表尾
    public void deleteLast() {
        if(!isEmpty()) {
            this.real = this.real.pre;
            if(this.size != 1) {
                this.real.next = null;
            } else {
                this.head = this.head.pre;
            }
            this.size --;
        }
    }

    //获得链表的节点个数
    public int size() {
        return this.size;
    }

    //判断链表是否为空
    public Boolean isEmpty() {
        return this.size <= 0 ? true : false;
    }

    //显示节点信息
    public void display() {
        if(size >0){
            Node node = head;
            int tempSize = size;
            if(tempSize == 1){//当前链表只有一个节点
                System.out.println("["+node.data+"]");
                return;
            }
            while(tempSize>0){
                if(node.equals(head)){
                    System.out.print("["+node.data+"->");
                }else if(node.next == null){
                    System.out.print(node.data+"]");
                }else{
                    System.out.print(node.data+"->");
                }
                node = node.next;
                tempSize--;
            }
            System.out.println();
        }else{//如果链表一个节点都没有，直接打印[]
            System.out.println("[]");
        }
    }

    public class Node{
        private Object data;
        private Node next;
        private Node pre;

        public Node(Object data) {
            this.data = data;
        }
    }

    public static void main(String[] args) {
        TwoWayLinkedList twoWayLinkedList = new TwoWayLinkedList();
        twoWayLinkedList.addHead(2);
        twoWayLinkedList.addLast(3);
        twoWayLinkedList.addHead(4);
        twoWayLinkedList.addLast(5);
        twoWayLinkedList.addHead(8);
        twoWayLinkedList.addLast(9);

        twoWayLinkedList.deleteHead();
        twoWayLinkedList.deleteLast();

        twoWayLinkedList.display();
    }
}
